// (C) Copyright to Kevin. Applied to MIT LICENSE

#include "BlockManager.h"

#include "Blocks/BlockDefinitions.h"

BlockManager* BlockManager::instance = NULL;

BlockManager::BlockManager()
{
	block_register = new TMap<uint16, Block*>();

	instance = this;
}

BlockManager::~BlockManager()
{
	// maybe we don't need to deconstruct those because they will unload only when quitting the game??? ;)
	// block_register->iterate([delete]);

	delete block_register;
}

void BlockManager::Initialize()
{
	Register(BlockAir::BLOCK_ID, new BlockAir());
	Register(BlockStone::BLOCK_ID, new BlockStone());
	Register(BlockDirt::BLOCK_ID, new BlockDirt());
	Register(BlockGrass::BLOCK_ID, new BlockGrass());
}

void BlockManager::Register(uint16 id, Block* block) {
	block_register->Add(id, block);
}

Block * BlockManager::Find(uint16 id)
{
	if (block_register->Find(id) == nullptr) return Find(BlockStone::BLOCK_ID);
	return *block_register->Find(id);
}

BlockManager * BlockManager::GetInstance()
{
	return instance;
}
